import Vuex from 'vuex';
import Vue from 'vue';

import Types       from './mutation-types';

import ui               from './modules/ui';
import backend               from './modules/backend';
import user               from './modules/user';

Vue.use(Vuex);
require("./middlewares");//加载logger

const successSuffix="_SUCCESS";
const errorSuffix="_FAILURE";

const state = {
  isLoading: false,
  direction: 'forward',

}

const mutations = {
   UPDATE_DIRECTION (state, direction) {
      state.direction = direction
    }

}

const store = new Vuex.Store({
    state,
    modules: {
        ui,
        backend,
        user
    },
    mutations
});
export const requestCallBackHandler=(response)=>{
    let isError=false,errorMsg='',actionType=response.type;
    if(response.status!=200||typeof response.data!='object'){
      isError=true;
      errorMsg=response.statusText;
    }else if(typeof response.data=='object'){
      if(response.data.code!=200){
          isError=true;
          errorMsg=response.data.message;
        }else{
          //成功抛出actionType+successSuffix
          store.dispatch(actionType+successSuffix,response.data.data);
        }
    }
    //错误抛出actionType+errorSuffix
    if(isError){
      store.dispatch(actionType+errorSuffix,{errorMsg:response.statusText});
    }
}
export default store;
